#include "TcpConnection.h"
int processRead(void *arg)
{
    struct TcpConnection *tcpConnection = (struct TcpConnection *)arg;
    // 接受数据
    int count = bufferSocketRead(tcpConnection->inbuffer, tcpConnection->channel->fd);
    Debug("收到的数据长度是%d \n",count);
    Debug("接收到的消息是\n%s", tcpConnection->inbuffer->data + tcpConnection->inbuffer->readpos);
    if (count > 0)
    {
        // 接收到了http请求 第二个参数是为了让这个文件描述符检测读事件
        int socket = tcpConnection->channel->fd;
#ifdef MSG_SEND_AUTO
        updateWriteEvent(tcpConnection->channel, true);                      // 设置读写属性
        eventAddTask(tcpConnection->evloop, tcpConnection->channel, MODIFY); // 把写属性在多路复用中开始检测
#endif
        bool flag = parseHttpRequest(tcpConnection->httprequest, tcpConnection->inbuffer, tcpConnection->httpResponse, tcpConnection->outbuffer, socket);
        if (!flag)
        {
            // 解析失败，回复一个简单的html
            const char *errMsg = "Http/1.1 400 Bad Request\r\n\r\n";
            bufferAppendString(tcpConnection->outbuffer, errMsg);
        }
    }
    else
    {
#ifdef MSG_SEND_AUTO
        // 断开连接
        eventAddTask(tcpConnection->evloop, tcpConnection->channel, DELETE);
#endif
    }
    // 断开连接
#ifndef MSG_SEND_AUTO
    eventAddTask(tcpConnection->evloop, tcpConnection->channel, DELETE);
#endif
    return 0;
}

int processWrite(void *arg)
{
    Debug("开始发送数据(基于事件发送)");
    struct TcpConnection *tcpConnection = (struct TcpConnection *)arg;
    // 发送数据
    int count = bufferSendData(tcpConnection->outbuffer, tcpConnection->channel->fd);
    if (count > 0)
    {
        // TODO 这里不是很懂
        // 判断是否数据完全的发送了
        if (getReadableCapacity(tcpConnection->outbuffer) == 0)
        {
            // 说明数据已经发完了 ，不在检测写事件,修改channel中保存的事件
            updateWriteEvent(tcpConnection->channel, false);
            // 修改dispatcher检测的集合， 添加任务节点
            eventAddTask(tcpConnection->evloop, tcpConnection->channel, MODIFY);
            // 删除这个节点
            eventAddTask(tcpConnection->evloop, tcpConnection->channel, DELETE);
        }
    }
    return 0;
}
struct TcpConnection *TcpConnectionInit(int fd, struct EventLoop *evloop)
{
    struct TcpConnection *tcpconnection = (struct TcpConnection *)malloc(sizeof(struct TcpConnection));
    tcpconnection->evloop = evloop;
    tcpconnection->inbuffer = bufferInit(10240);
    tcpconnection->outbuffer = bufferInit(10240);
    sprintf(tcpconnection->name, "connection-%d", fd);
    // http
    tcpconnection->httprequest = httpRequestInit();
    tcpconnection->httpResponse = httpResponseInit();

    tcpconnection->channel = ChannelInit(fd, ReadEvent, processRead, processWrite, tcpConnectionDestory, tcpconnection);
    eventAddTask(tcpconnection->evloop, tcpconnection->channel, ADD);
    Debug("和客户端建立连接 ，threadname %s threadid %lu , connection name: %s", evloop->threadName, evloop->threadId, tcpconnection->name);

    return tcpconnection;
}

int tcpConnectionDestory(void *arg)
{

    struct TcpConnection *tcpConnection = (struct TcpConnection *)arg;
    if (tcpConnection != NULL)
    {
        // 判断是否还有数据
        if (tcpConnection->inbuffer && getReadableCapacity(tcpConnection->inbuffer) == 0 && tcpConnection->outbuffer && (getReadableCapacity(tcpConnection->outbuffer) == 0))
        {
            // 销毁channel
            destoryChannel(tcpConnection->evloop, tcpConnection->channel);
            bufferDestory(tcpConnection->inbuffer);
            bufferDestory(tcpConnection->outbuffer);
            httpRequusetDestory(tcpConnection->httprequest);
            HttpResponseestory(tcpConnection->httpResponse);
            free(tcpConnection);
        }
    }
    Debug("连接断开。释放资源 gameove %s", tcpConnection->name);
    return 0;
}
